package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import sun.security.krb5.internal.crypto.Des;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2019/7/8.
 */
@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationMapper destinationMapper;

    @Autowired
    private RegionMapper regionMapper;

    @Override
    public List<Destination> getDestByDeep(int deep) {
        return destinationMapper.selectListByDeep(deep);
    }

    @Override
    public Object query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Destination> list = destinationMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Destination> ListByIds(Long[] refIds) {
        return destinationMapper.selectListByIds(refIds);
    }

    @Override
    public void changeHotValueById(Long id, Boolean hot) {
        destinationMapper.updateHotById(id, hot);
    }

    @Override
    public void delete(Long id) {
        destinationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void setInfo(Long id, String info) {
        destinationMapper.updateInfoById(id, info);
    }

    @Override
    public List<Destination> getToasts(Long parentId) {
        List<Destination> list = new ArrayList();
        creatToasts(parentId, list);
        Collections.reverse(list);
        return list;

    }

    @Override
    public List<Destination> getHotDestByRegionId(Long regionId) {
        if (regionId == -1) {
            //如果区域id为-1,代表需要查询国内的所有目的地
            return destinationMapper.selectByParentId(1L);
        }
        //根据区域的id,查询下面所有关联的子目的地
        //注意:这里目的地表中也有关联的区域id,为什么不去目的地表中,判断每条记录中的区域id是不是我手中的区域id
        //如果拿区域id去判断目的地表中的区域id,那么每条数据都会被检查,这样大大减低效率
        Region region = regionMapper.selectByPrimaryKey(regionId);
        Long[] refIds = region.getRefIds();
        return destinationMapper.selectListByIds(refIds);
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    private void creatToasts(Long parentId, List<Destination> list) {
        Destination destination = destinationMapper.selectByPrimaryKey(parentId);
        if (destination == null) {
            return;
        }
        list.add(destination);
        if (destination.getParent() != null) {
            creatToasts(destination.getParent().getId(), list);
        }
    }

    @Override
    public Destination getCountry(Long id) {
        Destination dest = destinationMapper.selectByPrimaryKey(id);
        if (dest.getParent() == null) {
            return dest;
        }
        List<Destination> toasts = this.getToasts(id);
        if (toasts != null && toasts.size() > 0){
            return toasts.get(0);
        }
        return null;
    }

    @Override
    public Destination getProvince(Long id) {
        if (this.isAbroad(id)){
            return null;
        }
        //国家 省份 城市
        Destination dest = destinationMapper.selectByPrimaryKey(id);
        if (dest.getDeep()==2){
            return dest;
        }
        if (dest.getDeep()==3){
            return dest.getParent();
        }
        return null;
    }

    @Override
    public List<Destination> getDestByOneDeep(int deep) {
        return destinationMapper.getDestByOneDeep(deep);
    }



    private boolean isAbroad(Long id) {
        Destination dest = destinationMapper.selectByPrimaryKey(id);
        if (dest==null){
            return false;
        }
        if ("中国".equals(dest.getName())){
            return false;
        }
        if (dest.getParent()!=null){
            if ("中国".equals(dest.getParent().getName())){
                return false;
            }
        }else {
            return true;
        }
        return isAbroad(dest.getParent().getId());
    }
}
